package 抽象外观模式.文件操作;

import java.io.*;

/**
 * 读取文件内容保存到字符串中
 */
public class FileReader {
    public String read(String fileNameSrc){
        System.out.println("读取文件");
        //1.创建源
        File src = new File(fileNameSrc);
        //2.选择流
        Reader is=null;
        StringBuilder str;
        try {
            is = new InputStreamReader(new FileInputStream(src));
            //3.操作资源
            char[] chars = new char[1024 * 10];
            int len=-1;
            str=new StringBuilder();
            while ((len=is.read(chars))!=-1){
                //读出来的追加到string后面
                str.append(chars);
            }
            return str.toString();//返回读取的字符串





        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }finally {
            try {
                if (is!=null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
